package org.example.security.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.common.utils.DateTimeUtil;
import org.example.db.entity.RolePermission;
import org.example.db.mapper.RolePermissionMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RolePermissionService {

    RolePermissionMapper rolePermissionMapper;

    public RolePermissionService(RolePermissionMapper rolePermissionMapper) {
        this.rolePermissionMapper = rolePermissionMapper;
    }

    public int addRolePermission(Long roleId, Long permissionId){

        rolePermissionMapper.delete(new QueryWrapper<RolePermission>().
                lambda().eq(RolePermission::getRoleId, roleId).eq(RolePermission::getPermissionId, permissionId));

        String time = DateTimeUtil.getDateTimeString();

        return rolePermissionMapper.insert(new RolePermission(null, roleId, permissionId, time, time));
    }

    public List<RolePermission> getRolePermission(Long RoleId){
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<RolePermission>();
        wrapper.lambda().eq(RolePermission::getRoleId,RoleId);
        return rolePermissionMapper.selectList(wrapper);
    }

    public void deletePermissions(Long roleId){
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<RolePermission>();
        wrapper.lambda().eq(RolePermission::getRoleId,roleId);
        rolePermissionMapper.delete(wrapper);
    }
}
